<template>
  <div class="reportMistakeInfoAction el-card is-always-shadow el-card__body">
    <avue-crud
      ref="waitSubmitMainCurd"
      :option="option"
      :table-loading="listLoading"
      :data="tableData"
      :page.sync="page"
      :search.sync="searchForm"
      @search-change="handleFilter"
      @selection-change="selectionChange">
      <template slot="menuRight">
        <el-button type="primary" size="mini" v-if="isShowLongPeriodSettlementBtn && isRunningFlag" @click="longZQJS()">提交</el-button>
        <el-button type="primary" size="mini" v-if="isShowLongPeriodSettlementBtn==false && isRunningFlag" @click="submitBomData()">普通提交</el-button>
        <el-button type="primary" size="mini" @click="exportData">导出</el-button>
      </template>

      <template slot="searchMenu" slot-scope="{row,size}">
        <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm)}" icon="el-icon-delete">清空</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
<!--        <el-button type="text" size="small" plain @click.stop="preview(scope.row,1)">预览</el-button>-->
        <el-button type="text" size="small" plain @click.stop="previewAffirm(scope.row)">预览</el-button>
      </template>
    </avue-crud>
<!--    <report-mistake-info-editor ref="reportMistakeInfoEditor" v-if="isShowReportMistakeInfoEditor"></report-mistake-info-editor>-->
    <preview ref="preview"></preview>
    <longZQJS ref="longZQJS"></longZQJS>
    <affirm ref="affirm"></affirm>
    <checkAccount ref="checkAccount" :params="{roleCode:'ZBZZ',isCheckRole:true}" @checkSubmitSuccess="checkSubmitSuccess"></checkAccount>

  </div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import ExportJsonExcel from "js-export-excel";
    import preview from './preview.vue';
    import longZQJS from './longZQJS.vue';
    import affirm from './affirm.vue';
    import checkAccount from '../checkAccount.vue';
    import moment from 'moment';
    import {
      getgetStationCodes,
      getLineCodes, getStationByCode, isLongPeriodSettlement, setSearchFiledsEmpty,getAutoSubmitFlag
    } from "../../../../api/subwayRat/commonCheck";
    import {exportDataToExcel} from "../../../../api/subwayRat/exportSelf";
    import {
      bomSubmitTvmData, getBomNotConfirmDatas,
      getPreview,
      getXw_bom_shouJieSuan_waitSubmit,
      waitSubmitMainOptionList
    } from "../../../../api/stationTiecketManager/shouRuJieSuan/bom/waitSubmitMain";
    import {get_sw_ck_data_submit_date} from "../../../../api/stationTiecketManager/shouRuJieSuan/tvm/waitSubmitMain";
    export default {
      components:{preview,longZQJS,checkAccount,affirm
      },
      data() {
            return {
              row : {},
              listLoading: true,
              isRunningFlag: true,//表示是否正在自动提交
              fileTemp:{},
              searchForm:{},
              tableData:[],
              page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20, // 每页显示多少条,
                isAsc: false// 是否倒序
              },
              autoWidth: true,
              downloadLoading: false,
              isShowLongPeriodSettlementBtn:false,//是否展示长周期结算按钮
              multipleSelectionRows:[],
              multipleSelectionKeys:[],
              reportNameIds:[],
              attendancePersonJobNo:null,//值班站长
              option:waitSubmitMainOptionList(),
            }
        },
        async created() {
          let lineCode=this.$store.getters.userInfo.lineCode;
          let stationCode=this.$store.getters.userInfo.stationCode;
          //获取结算日期
          let dates=await get_sw_ck_data_submit_date({lineCode:lineCode,stationCode:stationCode});
          if(dates.data.data && dates.data.data.length>0){
            this.searchForm.startSettleDate=dates.data.data[0].startSettleDate;
            this.searchForm.endSettleDate=dates.data.data[0].endSettleDate;
          }
          //
          this.search();
          this.isHaveBomNotConfirmDatas();
        },
        methods: {
          moment,setSearchFiledsEmpty,
          async exportData(){ // 导出
            this.searchForm.pageNo=1;
            this.searchForm.pageSize=1000000000;
            if(this.selectKeys.length>0){
              exportDataToExcel(this.selectKeys,this.option.column,'tvm结算-待提交主页');
            }else {
              let res=await getXw_bom_shouJieSuan_waitSubmit(this.searchForm);
              let data = res.data.data;
              exportDataToExcel(data,this.option.column,'tvm结算-待提交主页');
            }
          },
          search(){
            Object.assign(this.searchForm,this.$refs.waitSubmitMainCurd.searchForm);
            Object.assign(this.searchForm,this.page);
            this.searchForm.userCode=this.$store.getters.userInfo.userCode;
            this.searchForm.lineCode=this.$store.getters.userInfo.lineCode;
            this.searchForm.stationCode=this.$store.getters.userInfo.stationCode;
            this.searchForm.is_not_settle_status=2; //非等于2的情况
            this.searchForm.approvalStatus=0;
            if(this.searchForm.startSettleDate && this.searchForm.endSettleDate){
              getXw_bom_shouJieSuan_waitSubmit(this.searchForm).then((res)=>{
                this.tableData=res.data.data;
                this.listLoading = false;
                this.page.total=res.data.total;
              });
            }
          },
          longZQJS(){
            this.$nextTick(()=>{
              this.$refs.longZQJS.searchForm={startTime:this.searchForm.startSettleDate,endTime:moment(this.searchForm.endSettleDate).add(-1,'days').format("YYYY-MM-DD")+" 01:59:59"};
              this.$refs.longZQJS.dialogVisible=true;
              this.$refs.longZQJS.search();
            });
          },
          preview(row,type){
              this.$nextTick(()=>{
                row=getPreview(row);
                row.settleStartTime=this.searchForm.startSettleDate;
                row.settleEndTime=this.searchForm.endSettleDate;
                this.$refs.preview.search(row,type);
              });
          },

          previewAffirm(row){
            this.$nextTick(()=>{
              // row=getPreview(row);
              this.$refs.affirm.search(row);
            });
          },
          submitBomData(cab,callback){ //提交
            if(this.multipleSelectionRows.length<=0){
              this.$message.warning("请选择需要提交的数据！");
              return;
            }if(this.$refs.checkAccount.dialogVisibleAccountPassword==false){
              this.$refs.checkAccount.accountParamForm.account = '';
              this.$refs.checkAccount.accountParamForm.password = '';
              this.$refs.checkAccount.accountParamForm.token = '';
              this.$refs.checkAccount.dialogVisibleAccountPassword=true;
              this.$message.warning("请确认后可提交");
              return;
            }
            // if(!this.attendancePersonJobNo){
            //   this.$message.warning("请确认后可提交");
            //   this.$refs.checkAccount.dialogVisibleAccountPassword=true;
            //   return;
            // }
            let startSettleDate=moment(this.searchForm.startSettleDate).format('YYYY-MM-DD')+" 02:00:00";
            let endSettleDate=moment(this.searchForm.endSettleDate).add(1,'days').format("YYYY-MM-DD")+" 01:59:59";
            this.searchForm.approvalStatus=0;
            bomSubmitTvmData({attendancePersonJobNo:this.attendancePersonJobNo,
              // ids:this.multipleSelectionKeys.join(','),
              params:JSON.stringify(this.multipleSelectionRows),
              startTime:startSettleDate,endTime:endSettleDate,
            }).then(res=>{
              cab(res)
              // if(res.data.code==0){
              //   this.$message.success("操作成功");
              //   this.search();
              // }else{
              //   this.$message.error("操作失败");
              // }
              this.search();
            }).catch(error=> {
                callback(error)
                this.search();
                //this.$message.error("操作失败");
            })
          },
          checkSubmitSuccess(res,cab,callback){
            this.attendancePersonJobNo=res;
            this.submitBomData(cab,callback);
          },
          handleSelectionChange(val,a){
            this.multipleSelectionRows=val;
            this.multipleSelectionKeys=[];
            for(let i=0;i<this.multipleSelectionRows.length;i++){
              this.multipleSelectionKeys.push(this.multipleSelectionRows[i].id);
            }
          },
          selectionChange(list){
            this.multipleSelectionRows=list;
            this.multipleSelectionKeys=[];
            for(let i=0;i<list.length;i++){
              this.multipleSelectionKeys.push(list[i].id);
            }
          },
          handleFilter(params, done) {
            Object.assign(this.searchForm,params);
            this.search()
            done()
          },
          isHaveBomNotConfirmDatas(){ //是否含有待确认数据
            let param={};
            param.startSettleDate=this.searchForm.startSettleDate;
            param.endSettleDate=this.searchForm.endSettleDate;
            param.lineCode=this.$store.getters.userInfo.lineCode;
            param.stationCode=this.$store.getters.userInfo.stationCode;
            getBomNotConfirmDatas(param).then(res=>{
              if(res.data.code==0){
                if(res.data.data.beiYongJins>0 || res.data.data.cksws>0 || res.data.data.spyrjs>0 || res.data.data.qtxgsj>0 || res.data.data.ckswthp>0){
                  let msg="值站未确认提示信息：";
                  if(res.data.data.beiYongJins){
                    msg+="车站配票与配备用金页面："+res.data.data.beiYongJins+"个未确认；";
                  }
                  if(res.data.data.spyrjs){
                    msg+="售票员日结算数据录入页面："+res.data.data.spyrjs+"个未确认；";
                  }
		  /*
                  if(res.data.data.yingXioaChePiaos){
                    msg+="营销车票数据录入页面："+res.data.data.yingXioaChePiaos+"个未确认；";
                  }*/
                  if(res.data.data.cksws){
                    msg+= "乘客事务数据录入页面："+res.data.data.cksws+"个未确认；";
                  }
                  if(res.data.data.qtxgsj){
                    msg+="其他相关数据录入页面："+res.data.data.qtxgsj+"个未确认；";
                  }
                  if(res.data.data.ckswthp){
                    msg+="乘客事务退换票页面："+res.data.data.ckswthp+"个未确认；";
                  }
                  this.$message.error(msg);
                }
              }
            })
          }
        },
      mounted() {
        //此处多次调用
        //this.search();
        //是否展示长周期结算按钮
        isLongPeriodSettlement({lineCode:this.$store.getters.userInfo.lineCode,stationCode:this.$store.getters.userInfo.stationCode}).then(res=>{
          this.isShowLongPeriodSettlementBtn=res.data.data;
        });
        //是否正在自动提交
        getAutoSubmitFlag({lineCode:this.$store.getters.userInfo.lineCode,stationCode:this.$store.getters.userInfo.stationCode}).then(res=>{
          if(res.data.code==0){
              if(res.data.data){
                let msg="系统正在自动提交，请勿进行操作！！！";
                this.$message.warning(msg);
                this.listLoading = false;
                this.isRunningFlag = false;
            }
          }
        })
      }
    }
</script>
<style lang="scss">

</style>
